var serverData = {};
var serverTable;
jQuery(document).ready(function () {
	serverTable = jQuery('#dataTable').DataTable({
        "serverSide": true,
        "ordering": false,
        "ajax": {
            "dataType": "json",
            "type": "get",
            "cache": false,
            "url":window.rootPath+ '/ui/ScadaModelBaseInfo/getpage',
            "data": function (d) {
                return $.extend({}, d, serverData);
            }
        },
        columns: [
            {
            	data: null
            },
            {
	            data: null,
	            defaultContent: "",
	            render: function (data, type, full, meta) {
	            	var id=data.id
	            	var operate ='<input type="checkbox" class="checkchildContract">';
	            	return operate;
	            }   
            },
            {
                data: 'modelName'
            },
            {
            	data: 'user.name'
            },
            {
                data: 'createTime',
                render: function (data, type, full, meta) {
                    if (data)
                        return moment(data).format('YYYY/MM/DD');
                    else
                        return '';
                }
            },
            {
                data: 'memo'
            }
            ],
        "drawCallback": function (oSettings) {
            var k = 0;
            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

            for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                k++;
            }
        }
    });
	
	jQuery('.searchBtn').on('click', function () {
		serverData.key = jQuery('#keyword').val();
		serverTable.draw();
	});
	jQuery('.resetBtn').on('click', function () {
		jQuery('#keyword').val("");
		serverData.key = "";
		serverTable.draw();
	});
	jQuery('.addscadamodelBtn').on('click', function () {
		window.location = window.rootPath+"/ui/ScadaModelBaseInfo/add";
	});
	jQuery('.editscadamodelBtn').on('click', function () {
		var selects = getSelect("#dataTable", 1);
		if(selects.length==0){
			alert("请选择要编辑的节点！");
		}else if(selects.length>1){
			alert("只能编辑一个节点！");
		}else if(selects.length==1){
			var id = selects[0].id;
			window.location = window.rootPath+"/ui/ScadaModelBaseInfo/edit?id=" + id;
		}
	});
	jQuery('.delscadamodelBtn').on('click', function () {
		var selects = getSelect("#dataTable", 1);
		if(selects.length==0){
			alert("请选择要删除的节点！");
		}else if(selects.length>1){
			alert("只能删除一个节点！");
		}else if(selects.length==1){
			var id = selects[0].id;
			$.springLemonDialog({
				content: '确认删除此节点？',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					if (index == 0) {
						var url=window.rootPath+"/ui/ScadaModelBaseInfo/deleteNote"+"?id="+ id;
						jQuery.ajax({
							url:encodeURI(url),
							cache:false,
							type : "get",
							dataType:"json",
							success:function(data){
								serverTable.draw();
							}
						});
					}
					$(this).closeDialog(modal);
				}
			}); 
		}
	});
});